<?php
 
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 karPenilaian_Student
	 karPenilaian_Student_add
	 karPenilaian_Student_edit
	 */
	class CommbookController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Orang Tua";
		public $subMenu = "";
		public $title = "";		
		public $loginUser;		
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("orangtua")){
				LoginUser::redirectToLoginPage("orangtua");
			}else{
				$this->loginUser = LoginUser::getUser();
			}
						
		}
	  				
		public function actionIndex(){
			LoginUser::getPermission("orangtua_commbook");
			$this->subMenu = "Commbook";
			$this->layout ="//layouts/ly_blank";
			$student = $this->loginUser ->student;
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");	
			$student_id=$student->id;	
			$department_id=$student->department_id;				
						
 			$module= $this->loginUser ->module;
			
			
			$datenow=date("Y-m-d");
			$tahunajarans = MTahunAjaran::model()->findAll("is_active = TRUE   and department_id=:departmentId and (:datenow between start_date and end_date)  order by id",array(":departmentId"=>$department_id,":datenow"=>$datenow));

			if ($tahunajarans == null){
				$tahun_ajaran_id == "";
			}else{	
				$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id", $tahunajarans[0]->id);
			}
			
			 
			$kelass = VStudentKelasTahunAjaran::model()->findAll("student_id=:student_id and tahun_ajaran_id=:tahun_ajaran_id  ",array(":student_id"=>$student_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
 
			if ($kelass == null){
				$kelas_tahun_ajaran_id == "";
			}else{
				$kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id == "" ? $kelass[0]["kelas_tahun_ajaran_id"] : $kelas_tahun_ajaran_id;
			}
			 
		 
			$data=array( 
					"tahun_ajaran_id"=>$tahun_ajaran_id,
					"kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,
					"student_id"=>$student_id,
					"student_name"=>"aa"
					);				
					
			$this->render('index',$data);	
 		}
		
		public function actionByCommbook(){		
			$student_id = Yii::app()->request->getParam("student_id","");
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
			$mcon= Yii::app()->db;
			$sqlall='SELECT C.id as id, C.create_date, C.parent_id, C.karyawan_id, C.student_id as student_id,subject,message,last_sender,read_status,VSKTA.department_id,VSKTA.tahun_ajaran_id,  kelas_tahun_ajaran_id, B.name FROM  `m_commbook` C  left join v_student_kelas_tahun_ajaran  VSKTA on VSKTA.student_id = C.student_id  left join m_karyawan B on C.karyawan_id=B.id where  C.id is not null and parent_id=0  ';
			$sqltask_count = $sqlall . " and  C.student_id=:student_id and  kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id  order by last_sender desc,  read_status desc  , id asc ";
			$mReportMonthCommands=$mcon->createCommand($sqltask_count);		
			$mReportMonthCommands->bindValue('student_id',$student_id); 	
			$mReportMonthCommands->bindValue('kelas_tahun_ajaran_id',$kelas_tahun_ajaran_id); 					
			$mReportMonthCommands=$mReportMonthCommands->queryAll();		
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $mReportMonthCommands;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}		 
				
		public function actionedit(){		
			$op = Yii::app()->request->getParam("op","add");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("karyawan_commbook_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$commbook = new MCommbook;
				$commbook->create_date = date("Y-m-d H:i:s");
				$commbook->update_date = date("Y-m-d H:i:s");					
				$commbook->parent_id = 0; 
				$commbook->karyawan_id = Yii::app()->request->getParam("karyawan_id",""); 
				$commbook->student_id = Yii::app()->request->getParam("student_id","");
				$commbook->subject = Yii::app()->request->getParam("subject","");
				$commbook->message = Yii::app()->request->getParam("message","");
				$commbook->last_sender = "Teacher";
				$commbook->read_status = 1;				 				
				if ($commbook->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($commbook->getErrors());
				}		 
			}else if ($op == "edit"){
				LoginUser::getPermission("karyawan_commbook_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");	
				$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$id));							
				$commbook_update->update_date = date("Y-m-d H:i:s");
				$commbook_update->last_sender = "Parent";
				$commbook_update->read_status = 1;				 				
				if ($commbook_update->save()){
					$commbook = new MCommbook;
					$commbook->create_date = date("Y-m-d H:i:s");
					$commbook->update_date = date("Y-m-d H:i:s");					
					$commbook->parent_id = $id; 
					$commbook->karyawan_id = 	$commbook_update->karyawan_id;
					$commbook->student_id = Yii::app()->request->getParam("student_id","");
					$commbook->subject = "Re: ". Yii::app()->request->getParam("subject","");
					$commbook->message = Yii::app()->request->getParam("message","");
					$commbook->last_sender = "Parent";
					$commbook->read_status = 1;				 				
					if ($commbook->save()){
						echo "<script>
							alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
							window.close();					
						</script>";
						exit;
					}else{
						print_r($commbook->getErrors());
					}			
				}	
			}else if ($op == "del"){
				if (!LoginUser::getPermission("karyawan_commbook_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_semester where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
			}
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}

	public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","0");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","0");			
			$student_id = Yii::app()->request->getParam("student_id","");	
			$student_sid = Yii::app()->request->getParam("student_sid","");	
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");				
	 
			
			$guru =VJadwalPelajaranKelasTahunAjaran::model()->findAll(array("condition"=>"is_active = TRUE and kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id","order"=>"kelas_tahun_ajaran_id"));		
			if ($guru == null){
				$karyawan_id == "";
			}else{
				$karyawan_id = $karyawan_id == "" ? $guru[0]->karyawan_id : $karyawan_id;
			}							
			
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"gurus"=>$guru,"student_id"=>$student_id,"student_sid"=>$student_sid,"kelas_tahun_ajaran_id" =>$kelas_tahun_ajaran_id,"karyawan_id" =>$karyawan_id,"subject"=>"","message"=>"","is_active"=>"1","data_view"=>"");
			
			if ($op == "add"){
				LoginUser::getPermission("orangtua_commbook_add");
				$data["op"] = "add";
				$data["callback"] = $callback;
			}else if($op =="edit"){				
				LoginUser::getPermission("orangtua_commbook_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;								
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				$data["data_view"]="<table class=message><tr><Td>From</td><td>Message</td></tr>";
				$last_id_cek= $id;				
				$rsTingkat = MCommbook::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				if (count($rsTingkat) > 0){
				$data["karyawan_id"] = $karyawan_id;				
				$data["subject"] = $rsTingkat[0]->subject;
				$data["student_id"] = $rsTingkat[0]->student_id;
				$last_sender_cek= $rsTingkat[0]->last_sender;
				$last_subject_cek= $rsTingkat[0]->subject;				
				$data["data_view"]=$data["data_view"]."<tr><td width=50  >".date_format(date_create($rsTingkat[0]->create_date), 'd-M-Y').'<br>'.$rsTingkat[0]->last_sender."</td><td valign=top><b>".$rsTingkat[0]->subject."</b><br>".$rsTingkat[0]->message."</td></tr>";
				}	
				
				$rsTingkat = MCommbook::model()->findAll(array("condition"=>"parent_id = $id","order"=>"id"));
				if (count($rsTingkat) > 0){
					foreach ($rsTingkat as $rs_Tingkat){
						$data["data_view"]=$data["data_view"]."<tr><td width=50  >".date_format(date_create($rs_Tingkat["update_date"]), 'd-M-Y').'<br>'.$rs_Tingkat["last_sender"]."</td><td valign=top>".$rs_Tingkat["message"]."</td></tr>";
						$last_id_cek= $rs_Tingkat["id"];
					}
				}								
 				$data["data_view"]=$data["data_view"]."</table>";
				if ($last_sender_cek=="Teacher"){	
					$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$id));							
					$commbook_update->update_date = date("Y-m-d H:i:s");
					$commbook_update->read_status = 0;		
					$commbook_update->subject = $last_subject_cek;									 				
						if ($commbook_update->save()){
							/*echo "<script>
								alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
								window.close();					
							</script>";
							exit;*/
						}else{
							print_r($commbook_update->getErrors());
						}	 
					$commbook_update = MCommbook::model()->find("id=:id",array(":id"=>$last_id_cek));							
					$commbook_update->update_date = date("Y-m-d H:i:s");
					$commbook_update->read_status = 0;		
					$commbook_update->subject = $last_subject_cek;									 				
					if ($commbook_update->save()){
						/*echo "<script>
							alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
							window.close();					
						</script>";
						exit;*/
					}else{
						print_r($commbook_update->getErrors());
					}	 					
				}
//				echo $data["data_view"];
//				exit;
				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
		
	protected function beforeRender($view){
		if ($this->subMenu != ""){
			$this->title = $this->mainMenu . " - " .$this->subMenu;
		}
		return true;
	}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>